//
//  WeekPageView.m
//  MindBox
//
//  Created by MS on 16-1-14.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "WeekPageView.h"
#import "UIView+Frame.h"
#import "SelectView.h"
#import "Select_Simple_View.h"
#import "WeekPageCell.h"
@interface WeekPageView ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,SelectViewDelegate>
@property (nonatomic,weak) UIScrollView * scrollView;
@property (nonatomic,weak) UITableView * dayTableView;
@property (nonatomic,weak) UITableView * monthTableView;
@property (nonatomic,weak) UITableView * weekTableView;
@property (nonatomic,weak) Select_Simple_View * selView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,assign) BOOL  animation;

@end

@implementation WeekPageView


-(void)setWeekData:(WeekPageModel *)weekData
{
    _weekData = weekData;
    
    self.scrollView.contentOffset =CGPointMake(self.width, 0);
    
    [self.weekTableView reloadData];
    [self.dayTableView reloadData];
    [self.monthTableView reloadData];
}

-(void)setIndex:(NSInteger)index
{
    _index = index;
    if (index!=-1) {
        NSIndexPath * indexPath =[NSIndexPath indexPathForRow:index inSection:0];
        [self.weekTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
    else{
        self.animation = NO;
        [self.selView selectIndex:0 animation:NO];
        self.animation = YES;
        }
        [self.dayTableView reloadData];

}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:scrollView];
        scrollView.pagingEnabled = YES;
        scrollView.tag = 1001;
        scrollView.delegate = self;
        scrollView.bounces = NO;
        scrollView.contentSize =CGSizeMake(self.width*3, 0);
        self.animation = YES;
       
        self.scrollView = scrollView;
        self.dayTableView = [self addOneTableViewWithTag:0];
        self.weekTableView =[self addOneTableViewWithTag:1];
        self.monthTableView =[self addOneTableViewWithTag:2];
    
        Select_Simple_View * selView = [[Select_Simple_View alloc] initWithFrame:CGRectMake(0, 0, self.width, 35)];
        [self addSubview:selView];
        selView.delegate = self;
        self.selView  = selView;
        selView.buttonsArray = @[@"日榜",@"周榜",@"月榜"];
        [selView selectIndex:1 animation:NO];
    }
    return self;
}


-(UITableView *)addOneTableViewWithTag:(NSInteger)tag
{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = tag;
    tableView.backgroundColor =[UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.scrollView addSubview:tableView];
    return tableView;
}


-(void)selectViewDidClick:(SelectView *)selectView index:(NSInteger)index
{
    if (self.animation) {
        [self.scrollView setContentOffset:CGPointMake(index*self.width, 0) animated:YES];
    }
    else{
        
        [self.scrollView setContentOffset:CGPointMake(index*self.width, 0) animated:NO];
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.tag==1001) {
        self.page = scrollView.contentOffset.x/self.width;
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag==1001) {
        [self.selView selectIndex:self.page animation:YES];
    }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (scrollView.tag==1001) {
        [self.selView selectIndex:self.page animation:NO];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num = 0;
    
    if (tableView.tag==0) {
        num = self.weekData.day.count;
    }
    if (tableView.tag==1) {
         num = self.weekData.week.count;
    }
    if (tableView.tag==2) {
        num = self.weekData.month.count;
    }
    
    return num;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Gift_ItemGoods * item = nil;
    
    if (tableView.tag==0) {
        item = self.weekData.day[indexPath.row];
    }
    if (tableView.tag==1) {
        item = self.weekData.week[indexPath.row];
    }
    if (tableView.tag==2) {
        item = self.weekData.month[indexPath.row];
    }
    
   WeekPageCell * cell = [WeekPageCell cellWithcollectionView:tableView];
    
   
    
    if (indexPath.row==0) {
        cell.rankView.text = @"榜首";
        cell.rankView.font = [UIFont boldSystemFontOfSize:11];
        cell.rankView.textColor = [UIColor whiteColor];
        cell.rankBaseView.backgroundColor = [UIColor colorWithRed:0.98f green:0.36f blue:0.36f alpha:1.00f];
    }
    else{
        cell.rankView.font = [UIFont systemFontOfSize:9];
        cell.rankView.textColor = [UIColor redColor];
        cell.rankBaseView.backgroundColor = [UIColor whiteColor];
         cell.rankView.text = [NSString stringWithFormat:@"第%ld名",indexPath.row+1];
        
    }
    
    cell.item = item;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 130;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Gift_ItemGoods * item = nil;
    
    if (tableView.tag==0) {
        item = self.weekData.day[indexPath.row];
    }
    if (tableView.tag==1) {
        item = self.weekData.week[indexPath.row];
    }
    if (tableView.tag==2) {
        item = self.weekData.month[indexPath.row];
    }
    
    if ([self.delegate respondsToSelector:@selector(weekPageDidClick:goods:index:)]) {
        [self.delegate weekPageDidClick:self goods:item index:indexPath.row];
    }
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.frame = CGRectMake(0, 35, self.width, self.height-35);
    self.dayTableView.frame = CGRectMake(0, 0, self.width, _scrollView.height);
    self.weekTableView.frame = CGRectMake(self.width, 0, self.width, _scrollView.height);
    self.monthTableView.frame = CGRectMake(2*self.width, 0, self.width, _scrollView.height);
}


@end
